package com.mizhiquan.controller.front;

import com.mizhiquan.common.Const;
import com.mizhiquan.common.ServerResponse;
import com.mizhiquan.pojo.User;
import com.mizhiquan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户Controller
 */
@Controller//Spring注解->这是一个Controller
@RequestMapping("/user/")//SpringMVC注解 -> 拦截的链接 这里UserControll要拦截所有/user/开头的链接
public class UserController {

    /**
     * 依赖注入
     * 将IUserService注入进来
     * Spring会自动装配IUserService的实现类也就是UserServiceImpl
     */
    @Autowired
    private IUserService iUserService;

    //用户登陆
    @RequestMapping(value = "login.do",method = {RequestMethod.POST})   //SpringMVC注解 -> 拦截的链接 这里拦截/user/开头的login.do method参数定义request提交类型
    @ResponseBody//表示该方法的返回结果直接写入HTTP response body中 这里就将我们封装好的JSON序列号好的ServerResponse类返回给前端
    public ServerResponse<User> login(String username, String password,HttpSession session) {
        ServerResponse<User> response = iUserService.login(username,password);
        if (response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    //用户登出
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccessMessage("退出成功");
    }

    //用户注册
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user, HttpSession session){
        return iUserService.register(user);
    }

    //信息校验
    @RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String type,String value){ return  iUserService.checkValid(value,type);}

    //修改密码
    @RequestMapping(value = "change_pass.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> changePass(String oldpass,String newpass){ return iUserService.changePass(oldpass,newpass);}

    //手机、邮箱的激活
    @RequestMapping(value = "activate.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> activate(String type,String account,String token,String id){ return iUserService.Activate(type,account,token,id);}
}